package com.gopay.scheduler.job;

import com.gopay.common.constants.SubAccountConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.acct.CommonQueryDAO;
import com.gopay.common.domain.cps.transfer.Trans00606VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.order.MainOrderField;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import com.gopay.scheduler.dao.ReceiptOrderDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description  分账收取手续费失败的补偿任务
 * 1、触发延时分账收取手续费00606
 * @author Chenyu Li
 * @date 2018/4/8 15:49
 * Copyright (c) 2018. www.gopay.com.cn Inc.All rights reserved.
 */
public class SubAcctFeeJob extends GopayBaseJob {
    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(SubAcctFeeJob.class);

    @Resource(name = "comQueryDao")
    private CommonQueryDAO commonQueryDAO;

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;


    @Override
    public void doJobInternal() {
        logger.info("分账手续费补偿任务开始查询待收手续费的网关交易记录!");
        //1.查询一天的待收款分账的记录
        List<Object[]> result = commonQueryDAO.findBySql(buildSplitSql());

        //2.处理结果集
        if(result != null && result.size() > 0){
        logger.info("分账手续费补偿任务开始查询待收手续费的网关支付交易的记录数 = " + result.size());
            for (int i = 0; i < result.size(); i++) {
                try {
                    Object[] list = result.toArray();
                    String gopayOrderId = list[i].toString();
                    Trans00606VO vo = new Trans00606VO();
                    //00100的订单号
                    vo.setDtlSq(gopayOrderId);
                    vo.setIsForceRecv(SubAccountConstants.SPLIT_Force_0);
                    vo.setTranCode(IntTxnCd._00606.value);
                    vo.setRemark("收款分账");
                    logger.info("分账手续费补偿任务开始查询待收手续费处理第【" + (i + 1) + "】条记录，调用cps，网关支付交易流水号=" + gopayOrderId);
                    CPSResponse response = callCPSHelper.callByVOTicket(vo);
                    if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                        logger.error("------分账手续费补偿任务开始查询待收手续费调用cps返回异常  response=" + response.getRespCode());
                    }
                    logger.info("分账手续费补偿任务开始查询待收手续费第" + (i + 1) + "条处理成功,网关支付交易流水号=" + gopayOrderId);
                } catch (GopayException e) {
                    logger.error("分账手续费补偿任务开始查询待收手续费任务Gopay异常", e);
                } catch (Exception e) {
                    // 吃掉所有异常
                    logger.error("分账手续费补偿任务开始查询待收手续费任务发生未知异常", e);
                }
            }
        }
        logger.info("分账手续费补偿任务开始查询待收手续费任务结束");

    }

    private String buildSplitSql(){
        String sql = "select a.gopay_order_id  from cps_gen_main_order a left join cps_gen_receipt_order b " +
                "    on a.gopay_order_id = b.gopay_order_id" +
                " where a.txn_sta_cd = 20000" +
                "   and b.OTHER_RECV_STATUS = '2'" +
                "   and b.IS_DEDUCT_FEE_AMT = '0'" +
                "   and a.gopay_int_txn_cd = '00100'" +
                "   and a.gopay_txn_tm > sysdate-30" +
                "   and b.sub_acct_finish_time > sysdate - 30";
        return sql;
    }

}
